package com.spring.sunflower;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.bean.VersionBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.EnvironmentChoiceDialog;
import com.spring.sunflower.dialog.OnPermissionIntroClickListener;
import com.spring.sunflower.dialog.PersonInfoIntroDialog;
import com.spring.sunflower.http.AppRetrofit;
import com.spring.sunflower.login.DoorActivity;
import com.spring.sunflower.login.LoginByPhoneFruitActivity;
import com.spring.sunflower.login.LoginContract;
import com.spring.sunflower.login.SplashPresenter;
import com.spring.sunflower.util.AppUtils;
import com.spring.sunflower.util.ChannelInfoUtils;
import com.tencent.imsdk.v2.V2TIMManager;

/**
 * Created by Kevin on 2021/5/19<br/>
 * <p>
 * Describe:<br/>
 */
public class SplashActivity extends BaseActivity<SplashPresenter> implements LoginContract.SplashView {

    private String token;
    private String userID;
    private String currentEnvironment;
    private EnvironmentChoiceDialog dialog;
    private String mTempTicket = "";

    @Override
    public void doSomethingBeforeOnCreate() {
        super.doSomethingBeforeOnCreate();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public void initView() {
        if (!this.isTaskRoot()) {
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intent.ACTION_MAIN.equals
                        (action)) {
                    finish();
                    return;
                }
            }
        }
        currentEnvironment = getField(Constant.KEY_ENVIRONMENT, Constant.ENVIRONMENT_PRODUCT);
        if (currentEnvironment.equals(Constant.ENVIRONMENT_PRODUCT)) {//获取当前环境
            Constant.BASE_URL = Constant.BASE_URL_PRODUCT;
        } else if (currentEnvironment.equals(Constant.ENVIRONMENT_TEST)) {
            Constant.BASE_URL = Constant.BASE_URL_TEST;
        } else if (currentEnvironment.equals(Constant.ENVIRONMENT_LOCAL_TEST)) {
            String localEnvi = getField(Constant.KEY_ENVIRONMENT_LOCAL, "");
            if (TextUtils.isEmpty(localEnvi)) {
                Constant.BASE_URL = Constant.BASE_URL_LOCAL_TEST;
            } else {
                Constant.BASE_URL = localEnvi;
            }
        } else {
            Constant.BASE_URL = Constant.BASE_URL_PRODUCT;
        }
        saveField(Constant.KEY_ENVIRONMENT, currentEnvironment);
        token = getField(Constant.KEY_TOKEN, "");
        userID = getField(Constant.KEY_USERID, "");
        boolean agreement = getField(Constant.KEY_AGREEMENT, false);
        if (agreement) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    enterApp();
//                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
//                    startActivity(new Intent(SplashActivity.this, GenderSelectActivity.class));
                }
            }, 2000);
        } else {
            //弹窗个人信息引导弹窗
            personInfoIntroDialog();
        }
    }

    private void enterApp() {
        ((App)getApplication()).initSDK();
        //从显示到完全透明
        Animation fadeOut = AnimationUtils.loadAnimation(SplashActivity.this, R.anim
                .animation_fadeout);
        fadeOut.setFillAfter(true);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        boolean showSwitchEnvironmentDialog = getField(Constant.switchEnvironmentDialogShow, false);
        if (showSwitchEnvironmentDialog) {
            showEnvironmentDialog();
        } else {
            mPresenter.loginByDevice();

        }
//        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(userID)) {
//            mPresenter.loginByToken(userID, token);
//        } else {
//            startActivity(new Intent(SplashActivity.this, DoorActivity.class));
//            finish();
//        }
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_splash;
    }

    @Override
    public SplashPresenter setPresenter() {
        return new SplashPresenter(this);
    }

//    @Override
//    public void requestFinish() {
//        super.requestFinish();
//        startActivity(new Intent(SplashActivity.this, MainActivity.class));
//    }

    @Override
    public void requestFinishWithSth(String ticket) {
        super.requestFinishWithSth(ticket);
        mTempTicket = ticket;
        mPresenter.getVersion();
    }

    @Override
    public void showLoading() {
    }

    private void personInfoIntroDialog() {
        PersonInfoIntroDialog dialog = new PersonInfoIntroDialog(this, R.layout.dialog_user_info_private_intro, new OnPermissionIntroClickListener() {
            @Override
            public void onClickOK() {
                saveField(Constant.KEY_AGREEMENT, true);
                enterApp();
            }

            @Override
            public void onClickCancel() {
                PersonInfoIntroDialog introDialog = new PersonInfoIntroDialog(SplashActivity.this, R.layout.dialog_user_info_private_intro_2, new OnPermissionIntroClickListener() {
                    @Override
                    public void onClickOK() {
                        saveField(Constant.KEY_AGREEMENT, true);
                        enterApp();
                    }

                    @Override
                    public void onClickCancel() {
                        finish();

                    }
                }, false);
                new XPopup.Builder(SplashActivity.this)
                        .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                        .dismissOnTouchOutside(false)
                        .isRequestFocus(false)
                        .asCustom(introDialog)
                        .show();
            }
        }, true);
        new XPopup.Builder(this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .dismissOnTouchOutside(false)
                .isRequestFocus(false)
                .asCustom(dialog)
                .show();
    }

    private void showEnvironmentDialog() {
        dialog = new EnvironmentChoiceDialog(SplashActivity.this, new EnvironmentChoiceDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tvOK = dialog.findViewById(R.id.tvOK);
                tvOK.setText("选择好了");
            }

            @Override
            public void onClickCancel() {
                mPresenter.loginByDevice();
            }

            @Override
            public void onClickOK(String environment) {
                V2TIMManager.getInstance().unInitSDK();
                clearStorageUserData();
                // showToast(environment);
                AppRetrofit.destroy();
                if (environment.equals(Constant.ENVIRONMENT_PRODUCT)) {
                    Constant.BASE_URL = Constant.BASE_URL_PRODUCT;
                    saveField(Constant.KEY_ENVIRONMENT, environment);
//                    showToast("生产");
                } else if (environment.equals(Constant.ENVIRONMENT_TEST)) {
                    Constant.BASE_URL = Constant.BASE_URL_TEST;
                    saveField(Constant.KEY_ENVIRONMENT, environment);
//                    showToast("测试");
                } else {//本地测试
                    Constant.BASE_URL = environment;
                    saveField(Constant.KEY_ENVIRONMENT_LOCAL, environment);
                    saveField(Constant.KEY_ENVIRONMENT, Constant.ENVIRONMENT_LOCAL_TEST);
                }
//                saveField(Constant.KEY_ENVIRONMENT, environment);
                mPresenter.loginByDevice();
            }
        });
        new XPopup.Builder(SplashActivity.this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
//                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
    }

    @Override
    public void getVersionSuccess(VersionBean.DatasBean data) {
        int verCode = data.getVersionCode();
        String isVerify = data.getIsVerify();
        if (verCode == AppUtils.getVersionCode(this)){
            if (Constant.APP_REVIEWING.equals(isVerify)) {
//                Constant.isInCheckEnvironment = true;
                String mobile = getField(Constant.KEY_MOBILE, "");
                boolean hasClickLogout = getField(Constant.KEY_HAS_CLICK_LOGOUT, false);
                boolean isFirst = getField(Constant.KEY_FIRST_LOGIN, true);
                if (TextUtils.isEmpty(mobile) || hasClickLogout || isFirst) {
                    startNewActivity(LoginByPhoneFruitActivity.class);
                } else {
                    startActivity(new Intent(this, MainActivity.class));
                }
            }else{
                onNextActivity(Constant.PAY_TICKET.equals(mTempTicket));
            }
        }else{
            onNextActivity(Constant.PAY_TICKET.equals(mTempTicket));
        }
        finish();
    }

    @Override
    public void getVersionWithNull() {
        onNextActivity(Constant.PAY_TICKET.equals(mTempTicket));
        finish();
    }

    private void onNextActivity(boolean hasPayed){
        if (hasPayed) {
            startActivity(new Intent(this, MainActivity.class));
        } else {
            startActivity(new Intent(this, DoorActivity.class));
        }
    }
}
